package org.zoomdev.zoom.rpc.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zoomdev.zoom.rpc.utils.MessageEncoder;

public class RpcClientMessageToBytesEncoder extends MessageToByteEncoder {
    private static final Log log = LogFactory.getLog(RpcClient.class);

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf sendBuffer) throws Exception {
        if (o instanceof Completer) {
            Completer command = (Completer) o;
            sendBuffer.clear();

            byte[] bytes = command.bytes;
            int id = command.id;
            short func = command.func;
            if (log.isDebugEnabled()) {
                log.debug("发送消息 id:" + command.id + " func:" + command.func + " data:" + Hex.encodeHexString(command.bytes));
            }
            MessageEncoder.encode(sendBuffer, id, func, bytes);
        }
    }
}
